package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.ChangePasswordPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.PhonevalidationUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.RxTimerUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IChangePasswordView;

import butterknife.BindView;
import butterknife.OnClick;

public class ChangePasswordActivity extends BaseActivity<ChangePasswordPresenter> implements IChangePasswordView {

    @BindView(R.id.et_phone_number)
    EditText etPhoneNumber;
    @BindView(R.id.txt_get_yanzhengma)
    TextView txtGetYanzhengma;
    @BindView(R.id.et_yanzheng)
    EditText etYanzheng;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.view_bottomline)
    View viewBottomline;
    @BindView(R.id.rl_topview)
    RelativeLayout rlTopview;

    private String token;
    //1修改登录密码,2修改支付密码
    private String type;

    public static Intent createIntent(Context context, String type) {
        Intent intent = new Intent(context, ChangePasswordActivity.class);
        intent.putExtra("type", type);
        return intent;
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_change_password);
        token = SharedPreferencesUtil.getsInstances(this).getLoginInfo().getToken();
        type = getIntent().getStringExtra("type");
    }

    @Override
    protected void initPresenter() {
        mPresenter = new ChangePasswordPresenter(this);
    }

    @Override
    protected void init() {
        if (type.equals("1")) {
            txtTitle.setText("修改登录密码");
        } else {
            txtTitle.setText("修改支付密码");
        }
    }

    @OnClick({R.id.ll_leftview, R.id.txt_get_yanzhengma, R.id.btn_confirm})
    public void onViewClicked(View view) {
        String phoneNumber = etPhoneNumber.getText().toString().trim();
        String verifyCode = etYanzheng.getText().toString().trim();
        String password = etPwd.getText().toString().trim();
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.txt_get_yanzhengma:
                if (PhonevalidationUtil.isMobileNO(phoneNumber)) {
                    mPresenter.sendMsg(phoneNumber, "1");
                    RxTimerUtil.interval(1, 60, new RxTimerUtil.IRxNext() {
                        @Override
                        public void doNext(long number) {
                            txtGetYanzhengma.setClickable(false);
                            txtGetYanzhengma.setText(String.format(ResourcesUtils.getString(R.string.repeat_verify_code_hint), 60 - number));
                        }

                        @Override
                        public void doComplete() {
                            txtGetYanzhengma.setClickable(true);
                            txtGetYanzhengma.setText("验证");
                        }
                    });
                } else {
                    ToastUtils.showToast("手机号格式错误");
                }
                break;
            case R.id.btn_confirm:
                if (phoneNumber.equals("") || verifyCode.equals("") || password.equals("")) {
                    ToastUtils.showToast("信息填写不全");
                } else {
                    mPresenter.changePassword(token, phoneNumber, verifyCode, password);
                }
                break;
        }
    }

    @Override
    public void changeSuccess() {
        ToastUtils.showToast("修改密码成功");
        finish();
    }
}
